import time

from selenium.webdriver.common.by import By

from common.base import Base
from common.functions import open_browser
from page.frontpage.login_pageX import LoginPage


class ShoppingCart(Base, LoginPage):
    def login(self):
        self.max_window()
        self.get_index()
        self.input_username('srx')
        self.input_password('123456')
        self.login_button()
        time.sleep(2)

    # 进入购物车
    def get_cart(self):
        self.click((By.PARTIAL_LINK_TEXT, '购物车'))
        time.sleep(2)

    # 购物车添加商品
    def add_shop1(self):
        # 点击继续购物
        self.click((By.XPATH, "//a/img[@alt='continue']"))
        time.sleep(2)
        # 点击智能相机
        self.click((By.XPATH, "//div/img[@alt='智能相机']"))
        time.sleep(2)
        # 点击立即购买
        self.click((By.XPATH, "//td[@class='td1']/a/img"))
        time.sleep(2)

    # 购物车添加平衡车
    def add_shop2(self):
        self.click((By.XPATH, "//a/img[@alt='continue']"))
        time.sleep(2)
        self.click((By.XPATH, "//div/img[@alt='平衡车']"))
        time.sleep(2)
        self.click((By.XPATH, "//td[@class='td1']/a/img"))
        time.sleep(2)

    # 购物车添加自拍杆
    def add_shop3(self):
        self.click((By.XPATH, "//a/img[@alt='continue']"))
        time.sleep(2)
        self.click((By.XPATH, "//div/img[@alt='自拍杆']"))
        time.sleep(2)
        self.click((By.XPATH, "//td[@class='td1']/a/img"))
        time.sleep(2)

    def get_cart_text(self):
        return self.get_element_text((By.XPATH, "//div/a[@title='查看购物车']"))

    # 购物车增加商品数量以及更新购物车
    def alter_cart(self, number):
        # 清空数量输入框里的数据
        self.clear((By.XPATH, "//input[contains(@id,'goods_number_')]"))
        time.sleep(2)
        # 在数量输入框里输入要购买的数量
        self.send_keys((By.XPATH, "//tbody/tr[2]/td[5]/input"), number)
        time.sleep(2)
        # 点击更新购物车
        self.click((By.XPATH, "//td/input[2]"))
        time.sleep(3)

    # 购物车里删除商品
    def del_shop(self):
        self.click((By.XPATH, "//tbody/tr[2]/td[7]/a"))
        time.sleep(2)
        self.alert()

    # 把商品移出购物车放入收藏夹
    def collect(self):
        time.sleep(2)
        self.click((By.XPATH, "//tbody/tr[2]/td[7]/a[2]"))
        time.sleep(2)
        self.alert()
        time.sleep(2)

    # 清空购物车
    def clear_cart(self):
        self.click((By.XPATH, "//input[@value='清空购物车']"))


if __name__ == '__main__':
    sc = ShoppingCart(browser=open_browser())
    sc.login()
    sc.get_cart()
    sc.add_shop1()
    sc.add_shop2()
    sc.add_shop3()
    sc.get_cart_text()
    # sc.alter_cart(2)
    # sc.del_shop()
    # sc.collect()
    # sc.clear_cart()
    time.sleep(2)
    sc.quit()
